package week07

import (
	"fmt"
	"io"
	"sort"
)

// AcWing 145. 超市
// https://www.acwing.com/problem/content/147/

func main() {
	for {
		var n = -1
		_, err := fmt.Scanf("%d", &n)
		if err == io.EOF {
			break
		}
		if n == -1 {
			continue
		}
		var list = make([][]int, n)

		var maxDay = 0
		for i := 0; i < n; i++ {
			var price, expire = 0, 0
			_, _ = fmt.Scanf("%d", &price)
			_, _ = fmt.Scanf("%d", &expire)
			list[i] = []int{price, expire}
			maxDay = max(maxDay, expire)
		}
		infoSort(list).Sort()

		var father = make([]int, maxDay+1)
		for i := range father {
			father[i] = i
		}
		var ans = 0
		for i := 0; i < n; i++ {
			var day = find(father, list[i][1])
			if day > 0 {
				ans += list[i][0]
				unionSet(father, day-1, day)
			}
		}
		fmt.Println(ans)
	}
}

func find(father []int, x int) int {
	if father[x] != x {
		father[x] = find(father, father[x])
	}
	return father[x]
}

func unionSet(father []int, x, y int) {
	father[find(father, y)] = find(father, x)
}

type infoSort [][]int

func (is infoSort) Len() int { return len(is) }
func (is infoSort) Less(i, j int) bool { return is[i][0] > is[j][0] }
func (is infoSort) Swap(i, j int) { is[i], is[j] = is[j], is[i] }
func (is infoSort) Sort() { sort.Sort(is) }

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}